fn main() {
    let number = 13;
    match number {
        1 => println!("One"),
        2 => println!("Two"),
        3 | 4 => println!("Three or Four"),
        5..100 => println!("Between 5 and 100"),
        _ => println!("Anything"),
    }
    let b = false;
    match b {
        true => println!("True"),
        false => println!("False"),
    }

    //match 解构
    let tripe = (0, 2, 3);
    match tripe {
        (0, a, b) => println!("0 a={} b={}", a, b),
        (1, a, b) => println!("1 a={} b={}", a, b),
        _ => println!("Other"),
    }
    //match enum
    enum Color {
        // 这三个取值仅由它们的名字（而非类型）来指定。
        Red,
        Blue,
        Green,
        // 这些则把 `u32` 元组赋予不同的名字，以色彩模型命名。
        RGB(u32, u32, u32),
        HSV(u32, u32, u32),
        HSL(u32, u32, u32),
        CMY(u32, u32, u32),
        CMYK(u32, u32, u32, u32),
    }

    let color = Color::RGB(122, 17, 40);

    match color {
        Color::Red => println!("红色"),
        Color::Blue => println!("蓝色"),
        Color::Green => println!("绿色"),
        Color::RGB(r, g, b) => println!("红色: {}, 绿色: {}, 蓝色: {}", r, g, b),
        Color::HSV(h, s, v) => println!("色调: {}, 饱和度: {}, 明度: {}", h, s, v),
        Color::HSL(h, s, l) => println!("色调: {}, 饱和度: {}, 明度: {}", h, s, l),
        Color::CMY(c, m, y) => println!("青色: {}, 洋红: {}, 黄色: {}", c, m, y),
        Color::CMYK(c, m, y, k) => println!("青色: {}, 洋红: {}, 黄色: {}, 黑色: {}", c, m, y, k),
    }
    //指针 引用
    let reference = &4;
    match reference {
        &val => println!("Got a value {}", val),
    }
    // 如果不想用 `&`，需要在匹配前解引用。
    match *reference {
        val => println!("Got a value {}", val),
    }

    let _not_a_reference = 3;
    let ref is_a_reference = 3;

    let value = 5;
    let mut mut_value = 6;

    // 对于值 我们使用 `ref` 关键字来创建引用。
    match value {
        ref r => println!("Got a reference to a value {}", r),
    }

    // 对于可变值 我们使用 `ref mut` 关键字来创建引用。
    match mut_value {
        ref mut m => {
            *m += 10;
            println!("We added 10 to `{}`", m);
        }
    }
    //结构体解构
    struct Foo {
        x: (u32, u32),
        y: u32,
    }
    let foo = Foo { x: (1, 2), y: 3 };
    let Foo { x: (a, b), y: c } = foo;
    println!("a = {}, b = {}, c = {}", a, b, c);
    //忽略字段
    let Foo { y, .. } = foo;
    println!("y = {}", y);

    //卫语句
    let pair = (2, -4);
    match pair {
        (x, y) if x == y => println!("这两个数相等: {}", x),
        (x, y) if x + y == 0 => println!("这两个数sum=0"),
        (x, _) if x % 2 == 1 => println!("x是奇数"),
        _ => println!("其他情况"),
    }

    //绑定 @ 运算符

    fn age() -> u32 {
        15
    }
    match age() {
        0 => println!("Zero"),
        n @ 1..=12 => println!("介于1到12之间 {}", n),
        n @ 13..=19 => println!("介于13到19之间 {}", n),
        _ => println!("其他"),
    }

    fn some_numer() -> Option<i32> {
        Some(21)
    }
    match some_numer() {
        Some(n @ 21) => println!("找到了21 {}", n),
        Some(n) => println!("找到了其他 {}", n),
        None => println!("啥也没找到"),
    }
}
